
/**
------------------------------------------------------------
* NGUYEN SY BACH
* bit.mta@gmail.com
* Hoc Vien Ky Thuat Quan Su
------------------------------------------------------------
***	test source cua opencv
------------------------------------------------------------
*/
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

/// Global variables
const char* source_window = "Source image";
const char* warp_window = "Warp";
const char* warp_rotate_window = "Warp + Rotate";

/**
* @function main
*/
extern 
int test(char *filename)
{
	Point2f srcTri[3];
	Point2f dstTri[3];

	Mat rot_mat(2, 3, CV_32FC1);
	Mat warp_mat(2, 3, CV_32FC1);
	warp_mat.at<float>(0) = 1;
	warp_mat.at<float>(1) = 0;
	warp_mat.at<float>(2) = 0.f;
	warp_mat.at<float>(3) = 0;
	warp_mat.at<float>(4) = 1;
	warp_mat.at<float>(5) = 0.f;
	std::cout << warp_mat;
	Mat src, warp_dst, warp_rotate_dst;

	/// Load the image
	src = imread(filename, 1);

	/// Set the dst image the same type and size as src
	warp_dst = Mat::zeros(src.rows, src.cols, src.type());

	/// Set your 3 points to calculate the  Affine Transform
	/*srcTri[0] = Point2f(0, 0);
	srcTri[1] = Point2f(src.cols - 1.f, 0);
	srcTri[2] = Point2f(0, src.rows - 1.f);

	dstTri[0] = Point2f(src.cols*0.0f, src.rows*0.33f);
	dstTri[1] = Point2f(src.cols*0.85f, src.rows*0.25f);
	dstTri[2] = Point2f(src.cols*0.15f, src.rows*0.7f);*/

	/// Get the Affine Transform
	//warp_mat = getAffineTransform(srcTri, dstTri);

	/// Apply the Affine Transform just found to the src image
	warpAffine(src, warp_dst, warp_mat, warp_dst.size());

	/** Rotating the image after Warp */

	/// Compute a rotation matrix with respect to the center of the image
	Point center = Point(warp_dst.cols / 2, warp_dst.rows / 2);
	double angle = 90.0;
	double scale = 1;

	/// Get the rotation matrix with the specifications above
	rot_mat = getRotationMatrix2D(center, angle, scale);

	/// Rotate the warped image
	warpAffine(warp_dst, warp_rotate_dst, rot_mat, warp_dst.size());
	std::cout << center.x << " " << center.y << endl;
	std::cout << rot_mat << endl;

	/// Show what you got
	namedWindow(source_window, WINDOW_AUTOSIZE);
	imshow(source_window, src);

	namedWindow(warp_window, WINDOW_AUTOSIZE);
	imshow(warp_window, warp_dst);

	namedWindow(warp_rotate_window, WINDOW_AUTOSIZE);
	imshow(warp_rotate_window, warp_rotate_dst);

	/// Wait until user exits the program
	waitKey(0);

	return 0;
}